package class_sources;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jibble.simpleftp.SimpleFTP;
import org.ksoap2.serialization.PropertyInfo;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Log;

public class OtherMethods {

	  private static WSandJson wsHelper=new WSandJson();
	  private static String nameSpace="urn:ProductsService",url="http://www.codediary.net/service/roadship_service.php";
	  private static ArrayList<HashMap<String, String>> arrayList=new ArrayList<HashMap<String,String>>();
	  private SimpleFTP ftp=new SimpleFTP();
	  
	  public Bitmap GetBitmapFromUrl(String strUrl){
		  try {
	            //URL url = new URL(strUrl);
	            Log.i("Bitmap URL", strUrl);
	            HttpGet httpRequest = new HttpGet(URI.create(strUrl) );
	            HttpClient httpclient = new DefaultHttpClient();
	            HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
	            HttpEntity entity = response.getEntity();
	            BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
	            Bitmap myBitmap = BitmapFactory.decodeStream(bufHttpEntity.getContent());
	            if(myBitmap==null)
	            	Log.i("BITMAP ERROR", "myBitmap is NULL");
	            return myBitmap;
	        } catch (Exception e) {
	            e.printStackTrace();
	            Log.e("Bitmap ERROR",e.getMessage());
	            return null;
	        }
	  }
	  
	  public InputStream ResizeImage(String path)
	  {
		    Bitmap b= BitmapFactory.decodeFile(path);
		    Bitmap out = Bitmap.createScaledBitmap(b, 320, 320, false);
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			out.compress(CompressFormat.JPEG, 100, stream);
			InputStream is = new ByteArrayInputStream(stream.toByteArray());
		    try {
		          b.recycle();
		          out.recycle();
		          return is;
		        } 
		    catch (Exception e) {
		    	e.printStackTrace();
		    	return null;
		              }
	  }
	  public Boolean UploadImage(String path) throws Exception
	  {
		  InputStream is=ResizeImage(path);
		  ftp.connect("ftp.codediary.net",21,"codeynet","175160175160");
		  ftp.bin();
		  ftp.cwd("/httpdocs/service/profile_images");
		  Boolean bo=ftp.stor(is,new File(path).getName());
		  ftp.disconnect();
		  return bo;
	  }
	  
	  public String getCommand(String session)
	  {
		  try
		  {
			  PropertyInfo[] p=new PropertyInfo[1];
			  p[0]=new PropertyInfo();
			  p[0].setType(String.class);
			  p[0].setName("session");
			  p[0].setValue(session);
			  String res=wsHelper.CallWSFunction(nameSpace, url, "getCommand", "urn:User#getCommand", p);
			  ArrayList<HashMap<String, String>> arrayList=new ArrayList<HashMap<String,String>>();
			  arrayList=wsHelper.JsonToArrayList(res);
			  if(arrayList.get(0).get("Is_Command").contains("0"))
				  return "YOK";
			  if(!arrayList.get(0).get("Command_Message").equals("0") && !arrayList.get(0).get("Command_Message").equals(""))
				  return arrayList.get(0).get("Command")+"_"+arrayList.get(0).get("Command_Message");
			  else return arrayList.get(0).get("Command");
		  }
		  catch(Exception ex)
		  {
			  ex.printStackTrace();
			  Boolean res=SetOnlineStatus(session, Integer.toString(0));
			  if(!res)
				Log.i("OnlineStatus", "Set Online Status Method Error."); 
			  return "ERROR";
		  }
	  }
	  
	  public static Boolean SetOnlineStatus(String session,String onlineStatus)
	  {
		  	PropertyInfo[] pInfo=new PropertyInfo[2];
		    pInfo=new PropertyInfo[2];
			pInfo[0]=new PropertyInfo();
			pInfo[1]=new PropertyInfo();
			pInfo[0].setType(String.class);
			pInfo[1].setType(String.class);
			pInfo[0].setName("session");
			pInfo[0].setValue(session);
			pInfo[1].setName("onlineStatus");
			pInfo[1].setValue(onlineStatus);
			String result = wsHelper.CallWSFunction(nameSpace, url, "setUserOnlineStatus", "urn:User#setUserOnlineStatus",pInfo );
			arrayList=wsHelper.JsonToArrayList(result);
			if(arrayList.get(0).get("Result").contains("false"))
				return false;
			else return true;
	  }
	  
	  public Boolean SetCommand(String session,int is_command,String command,String command_message)
		{
		  try
		  {
		    PropertyInfo[] p=new PropertyInfo[4];
			p[0]=new PropertyInfo();
			p[1]=new PropertyInfo();
			p[2]=new PropertyInfo();
			p[3]=new PropertyInfo();
			p[1].setName("command");
			p[1].setValue(command);
			p[1].setType(String.class);
			p[2].setName("command_message");
			p[2].setValue(command_message);
			p[2].setType(String.class);
			p[0].setName("is_command");
			p[0].setValue(Integer.toString(is_command));
			p[0].setType(String.class);
			p[3].setName("session");
			p[3].setValue(session);
			p[3].setType(String.class);
			String result=wsHelper.CallWSFunction("urn:ProductsService", "http://www.codediary.net/service/roadship_service.php", "setCommand", "urn:User#setCommand",p);
			ArrayList<HashMap<String, String>> arrayList=new ArrayList<HashMap<String,String>>();
			arrayList=wsHelper.JsonToArrayList(result);
			if(arrayList.get(0).get("Result").contains("false"))
				return false;
			else
				return true;
		  }
		  catch(Exception ex)
		  {
			  ex.printStackTrace();
			  Boolean res=OtherMethods.SetOnlineStatus(session, Integer.toString(0));
				if(!res)
					Log.i("OnlineStatus", "Set Online Status Method Error."); 
			  return false;
		  }
	  }
	  
	  public String getEmail(Context context) {
	    AccountManager accountManager = AccountManager.get(context); 
	    Account account = getAccount(accountManager);

	    if (account == null) {
	      return null;
	    } else {
	      return account.name;
	    }
	  }

	  private static Account getAccount(AccountManager accountManager) {
	    Account[] accounts = accountManager.getAccountsByType("com.google");
	    Account account;
	    if (accounts.length > 0) {
	      account = accounts[0];      
	    } else {
	      account = null;
	    }
	    return account;
	  }
	}